/**
 * Created by tanweiping on 2018/7/31.
 */

window.onload = function () {
    getVerify(document.getElementById("imgVerify"));
}

function getVerify (img){
    img.src =  application.contextPath + "/login/getVerify?"+Math.random();
};

function login(form){
    var btn = document.getElementById("submit");
    btn.disabled = 'disabled';//只要点击就将按钮的可点击的状态更改为不可以点击的状态
    setTimeout(function(){//设置时间，多久可以改变状态为可以点击
        btn.disabled = '';
    },2000);
    var array = $(form).serializeArray();
    var data = {};
    for(var index in array){
        data[array[index]['name']] = array[index]['value']
    }
    $.post(
        //security/login方法在com.imc.core.config.websecurity.WebsecurityConfig
        application.contextPath+"/security/login",
        data,
        function (res) {
            if(null!=res){
                if(null == res.data){
                    layer.msg(res.message)
                    return false;
                }
                if("student" == res.data.user.duty){
                    layer.msg('权限不够，学生不可以登录本系统。')
                    return false;
                }
            }
            bootoast({
                message: res.message,
                type: res.isSuccess?'success':'warning',
                position:'right-top',
                timeout:2
            });
            setTimeout(function(){
                if (res.isSuccess){
                    //跳转主页
                    sessionStorage.setItem("user",JSON.stringify(res.data.user));
                    window.location.replace(application.contextPath+"/home/index");
                    return;
                }
                //alert(res.message);
                //form.reset()
                if (res.code == '-100'){
                    $(form).find("[name='password']").val("");
                }else if (res.code == '-101'){
                    form.reset();
                }
            },500)

        }
    )
    return false;
}